import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import qs from 'qs'
import {
  getCurDate,
  setSessionStorage,
  getSessionStorage,
  removeSessionStorage,
  setLocalStorage,
  getLocalStorage,
  removeLocalStorage
} from './common.js'
const app = createApp(App);

app.config.productionTip = false;
axios.defaults.baseURL = 'http://localhost:14000/';
axios.interceptors.response.use(function(response){
	if(response.data.code==403){
		location.herf = '/error403';
	}
	return response;
},function(error){
	console.log(error);
	return Promise.reject(error);
});
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;

router.beforeEach((to, from, next) => {
 let user = sessionStorage.getItem('user');
 let business = sessionStorage.getItem('business');
  
 // 公共页面，不需要登录
 const publicPages = ['/', '/index', '/businessList', '/businessInfo', '/login', '/register', '/error403', '/businessLogin', '/businessRegister','/businessInfoEdit'];
 
 // 商家页面，需要商家登录
 const businessPages = ['/businessManage', '/foodManage', '/businessWallet'];
 
 // 判断是否是公共页面
 const isPublicPage = publicPages.includes(to.path);
 // 判断是否是商家页面
 const isBusinessPage = businessPages.includes(to.path);
  
 if (isPublicPage) {
   // 公共页面直接放行
   next();
 } else if (isBusinessPage) {
   // 商家页面需要商家登录
   if (business) {
     next();
   } else {
     next('/businessLogin');
   }
 } else {
   // 其他页面需要用户登录
   if (user) {
     next();
   } else {
     next('/login');
   }
 }
});


const mountedApp = app.use(router);
mountedApp.mount('#app');


